# -*- coding: utf-8 -*-
from odoo import api, fields, models
from  odoo.addons import decimal_precision as dp
# from odoo.fields import Date as dt
from odoo.exceptions import ValidationError
import odoo


class ExamData(models.Model):
    _name='exam.data'
    _order='date_exam desc,who'

    TC=5.2
    TG=1.7
    LDLC=3.37
    TCoverHDLC=4.5

    date_exam=fields.Date('体检日期')
    who=fields.Selection([('dbh','dbh'),('dxj','dxj')])
    glu=fields.Float('空腹血糖(mmol/l)',dp.get_precision('exam_data'))
    tc_mmoll=fields.Float('总胆固醇(mmol/l)',dp.get_precision('exam_data'),compute='_compute_unit_trans',inverse='_inv_trans',readonly=False,store=True)
    tg_mgdl=fields.Float('甘油三脂(mg/dl)',dp.get_precision('exam_data'))
    tg_mmoll=fields.Float('甘油三脂(mmol/l)',dp.get_precision('exam_data'),compute='_compute_unit_trans',inverse='_inv_trans',readonly=False,store=True)
    hdlc_mgdl=fields.Float('高密度胆固醇(mg/dl)',dp.get_precision('exam_data'))
    hdlc_mmoll=fields.Float('高密度胆固醇(mmol/l)',dp.get_precision('exam_data'),compute='_compute_unit_trans',inverse='_inv_trans',readonly=False,store=True)
    ldlc_mgdl=fields.Float('低密度胆固醇(mg/dl)',dp.get_precision('exam_data'))
    ldlc_mmoll=fields.Float('低密度胆固醇(mmol/l)',dp.get_precision('exam_data'),compute='_compute_unit_trans',inverse='_inv_trans',readonly=False,store=True)
    vldlc_mgdl=fields.Float('极低密度胆固醇(mg/dl)',dp.get_precision('exam_data'))
    vldlc_mmoll=fields.Float('极低密度胆固醇(mmol/l)',dp.get_precision('exam_data'),compute='_compute_unit_trans',inverse='_inv_trans',readonly=False,store=True)
    remark=fields.Char('备注:')
    # tc=fields.Float('总胆固醇指标',dp.get_precision('exam_data'),default=5.2)
    # tg=fields.Float('甘油三脂指标',dp.get_precision('exam_data'),default=1.7)
    # ldlc=fields.Float('低密度脂蛋白指标',dp.get_precision('exam_data'),default=3.37)
    tc_flag=fields.Boolean('未超标',compute='_compute_flag',store=True)
    tg_flag=fields.Boolean('未超标',compute='_compute_flag',store=True)
    ldlc_flag=fields.Boolean('未超标',compute='_compute_flag',store=True)
    tc_mgdl=fields.Float('总胆固醇(mg/dl)',dp.get_precision('exam_data'))
    tc_mmoll=fields.Float('总胆固醇(mmol/l)',dp.get_precision('exam_data'),compute='_compute_unit_trans',inverse='_inv_trans',readonly=False,store=True)
    tg_mgdl=fields.Float('甘油三脂(mg/dl)',dp.get_precision('exam_data'))
    tg_mmoll=fields.Float('甘油三脂(mmol/l)',dp.get_precision('exam_data'),compute='_compute_unit_trans',inverse='_inv_trans',readonly=False,store=True)
    hdlc_mgdl=fields.Float('高密度胆固醇(mg/dl)',dp.get_precision('exam_data'))
    hdlc_mmoll=fields.Float('高密度胆固醇(mmol/l)',dp.get_precision('exam_data'),compute='_compute_unit_trans',inverse='_inv_trans',readonly=False,store=True)
    ldlc_mgdl=fields.Float('低密度胆固醇(mg/dl)',dp.get_precision('exam_data'))
    ldlc_mmoll=fields.Float('低密度胆固醇(mmol/l)',dp.get_precision('exam_data'),compute='_compute_unit_trans',inverse='_inv_trans',readonly=False,store=True)
    vldlc_mgdl=fields.Float('极低密度胆固醇(mg/dl)',dp.get_precision('exam_data'))
    vldlc_mmoll=fields.Float('极低密度胆固醇(mmol/l)',dp.get_precision('exam_data'),compute='_compute_unit_trans',inverse='_inv_trans',readonly=False,store=True)
    remark=fields.Char('备注:')
    # tc=fields.Float('总胆固醇指标',dp.get_precision('exam_data'),default=5.2)
    # tg=fields.Float('甘油三脂指标',dp.get_precision('exam_data'),default=1.7)
    # ldlc=fields.Float('低密度脂蛋白指标',dp.get_precision('exam_data'),default=3.37)
    tc_flag=fields.Boolean('未超标',compute='_compute_flag',store=True)
    tg_flag=fields.Boolean('未超标',compute='_compute_flag',store=True)
    ldlc_flag=fields.Boolean('未超标',compute='_compute_flag',store=True)

    def _inv_trans(self):
        for r in self:
            r.tc_mgdl=r.tc_mmoll/0.02586
            r.tg_mgdl=r.tg_mmoll/0.01129
            r.hdlc_mgdl=r.hdlc_mmoll/0.02586
            r.ldlc_mgdl=r.ldlc_mmoll/0.02586
            r.vldlc_mgdl=r.vldlc_mmoll/0.02586
            
    @api.depends('tc_mmoll','tg_mmoll','ldlc_mmoll')
    def _compute_flag(self):
        for r in self:
            if r.tc_mmoll<=r.TC:
                r.tc_flag=True
            if r.tg_mmoll<=r.TG:
                r.tg_flag=True 
            if r.ldlc_mmoll<=r.LDLC:
                r.ldlc_flag=True   


    def name_get(self):
        result = []
        for record in self:
            result.append(
            (record.id,u"%s(%s)" % (record.who, record.date_exam)))
        return result

    @api.depends('tc_mgdl','tg_mgdl','hdlc_mgdl','ldlc_mgdl','vldlc_mgdl')
    def _compute_unit_trans(self):
        for r in self:
            if r.tc_mgdl:
                r.tc_mmoll=0.02586*r.tc_mgdl
            if r.tg_mgdl:
                r.tg_mmoll=0.01129*r.tg_mgdl
            if r.hdlc_mgdl:
                r.hdlc_mmoll=0.02586*r.hdlc_mgdl
            if r.ldlc_mgdl:
                r.ldlc_mmoll=0.02586*r.ldlc_mgdl
            if r.vldlc_mgdl:
                r.vldlc_mmoll=0.02586*r.vldlc_mgdl

    @api.model
    def default_get(self, fields):
        res = super(ExamData, self).default_get(fields)
        if 'who' in fields:
            if self.env['exam.data'].search([]):
                res['who']=self.env['exam.data'].search([]).sorted(lambda x: x.id, reverse=True)[0].who
        if 'date_exam' in fields:
            # odoo.fields.DATE_FORMAT="%Y-%m-%d"
            res['date_exam']=odoo.fields.Date.today()
        return res

    
   